package com.jf.controller;

import com.alibaba.fastjson.JSON;
import com.jf.bean.Banner;
import com.jf.bean.Enterprise;
import com.jf.service.EnterpriseService;
import com.jf.service.impl.EnterpriseServiceImpl;
import com.jf.util.MyUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;

@WebServlet("/enterprise")
public class EnterpriseServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String param = request.getParameter("param");
        BufferedReader in = new BufferedReader(new InputStreamReader(request.getInputStream(), StandardCharsets.UTF_8));
        String result = MyUtils.getRequestJson(in);
        Enterprise enterprise = JSON.parseObject(result,Enterprise.class);

        EnterpriseService enterpriseService = new EnterpriseServiceImpl();

        if(MyUtils.isStringEmpty(param)){
            request.getRequestDispatcher("/WEB-INF/pages/admin/enterprise/enterprise-edit.jsp").forward(request,response);
        }
        if("info".equals(param)){
            response.getWriter().println(JSON.toJSON(enterpriseService.getInfo()));
        }
        if("update".equals(param)){
            response.getWriter().println(JSON.toJSON(enterpriseService.update(enterprise)));
        }
    }
}
